/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogoConselheiro.java
 *
 * Created on 08/07/2010, 15:56:40
 */
package adcon.apresentacao;

import adcon.negocio.condominio.Condominio;
import adcon.negocio.condomino.Condomino;
import adcon.persistencia.DAO;
import avant.view.AvantViewUtil;
import avant.view.model.AvantComboModel;
import avant.view.model.AvantTableModel;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 *
 * @author Administrador
 */
public class DialogoConselheiro extends javax.swing.JDialog {

    private boolean ok;
    private AvantComboModel<Condomino> modeloCondominos;
    private List<Condomino> condominos;
    private Condominio condominio;
    private AvantTableModel<Condomino> modelo;
    private Condomino condomino;

    /** Creates new form DialogoConselheiro */
    public DialogoConselheiro(AvantTableModel modelo, Condominio condominio, java.awt.Frame parent) {
        super(parent, true);
        this.condominio = condominio;
        this.modelo = modelo;
        initComponents();
        this.setLocationRelativeTo(null);
        preencherTela();
        adicionarOuvintes();
    }

    private void adicionarOuvintes(){
        ControladorDeEventos controlador = new ControladorDeEventos();
        btnSalvar.addActionListener(controlador);
        btnCancelar.addActionListener(controlador);
    }

    private List listarCondominos() {
        return condominos = new DAO().listar(Condomino.class, "CondominoPorCondominio", condominio.getCodigo());
    }

    private void salvar() {
        ok = preencherobjeto();
        if(ok){
        new DAO().salvar(condomino);
        dispose();
        }else{
            AvantViewUtil.exibirErro("Não existem condôminos cadastrados!", this);
        }
    }

    private void cancelar(){
        ok = false;
        dispose();
    }

    private void preencherTela() {
        modeloCondominos = new AvantComboModel<Condomino>(cbCondôminos, listarCondominos());
        if(modeloCondominos.getSelectedItem() != null){
        condomino = modeloCondominos.getObjetoSelecionado();

        txtNumero.setText(condomino.getNumeroConselheiro());
        txtTipo.setText(condomino.getTipoConselheiro());
        }
        
    }

    private boolean preencherobjeto() {
        if (!modeloCondominos.getObjetos().isEmpty() ){
            System.out.println("I am here");
            condomino = modeloCondominos.getObjetoSelecionado();
            condomino.setConselheiro(true);
            condomino.setNumeroConselheiro(txtNumero.getText());
            condomino.setTipoConselheiro(txtTipo.getText());
            return true;
        }
        return false;
    }

    public static boolean editar(AvantTableModel modelo, Condominio condominio, Frame pai) {
        DialogoConselheiro dialogo = new DialogoConselheiro(modelo, condominio, pai);
        dialogo.setVisible(true);
        return dialogo.ok;
    }

    private class ControladorDeEventos implements ActionListener {

        public void actionPerformed(ActionEvent e) {
           Object origem = e.getSource();
           if(origem == btnSalvar){
                salvar();
           }else if(origem == btnCancelar){
                cancelar();
           }
        }




    }


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbCondôminos = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        txtNumero = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtTipo = new javax.swing.JTextField();
        btnCancelar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro Conselheiros");
        setResizable(false);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 102, 102), 1, true));

        cbCondôminos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Condômino:");

        jLabel2.setText("Número:");

        jLabel3.setText("Tipo:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbCondôminos, 0, 389, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTipo, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbCondôminos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(txtTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adcon/imagens/cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adcon/imagens/salvar.png"))); // NOI18N
        btnSalvar.setText("Salvar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(128, 128, 128)
                        .addComponent(btnSalvar)
                        .addGap(28, 28, 28)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cbCondôminos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtNumero;
    private javax.swing.JTextField txtTipo;
    // End of variables declaration//GEN-END:variables
}
